package com.zhangsun;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MybatisPlusGenerator {

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");

        FastAutoGenerator.create("url", "user", "password")
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author("chao chen").fileOverride().outputDir(projectPath + "/src/main/java").dateType(DateType.ONLY_DATE))
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent("com.zhangsun").moduleName(scanner.apply("请输入模块名"))
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper/")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .addTablePrefix(scanner.apply("请输入表前缀")))
                .strategyConfig((builder -> builder.entityBuilder().superClass(Model.class).enableTableFieldAnnotation().enableActiveRecord()))
                .strategyConfig(builder -> builder.controllerBuilder().enableRestStyle())
                .strategyConfig(builder -> builder.serviceBuilder().formatServiceFileName("%sService"))
                .strategyConfig(builder -> builder.mapperBuilder().enableBaseResultMap().enableBaseColumnList())
                .execute();

    }

    // 处理 all 情况
    private static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

}
